package cn.wen.base.view.limit;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import cn.wen.base.R;


/**
 * 底部的布局
 */

public class BottomLayout extends LinearLayout {


    private TabAdapter mAdapter;
    private int mPosition;
    private int mCenterHeight;
    private ViewPager mViewPager;

    public BottomLayout(Context context) {
        this(context, null);
    }

    public BottomLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public BottomLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BottomLayout);
        mCenterHeight = (int) array.getDimension(R.styleable.BottomLayout_bottom_center_height, 0f);
        array.recycle();
    }


    public void setAdapter(TabAdapter adapter, ViewPager viewPager) {
        this.mAdapter = adapter;
        this.mViewPager = viewPager;
        if (adapter == null) {
            return;
        }
        removeAllViews();
        int count = adapter.getCount();
        for (int i = 0; i < count; i++) {
            final View view = adapter.getView(i, this);
            LayoutParams lp = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
            lp.gravity = Gravity.BOTTOM;
            if (mCenterHeight > 0 && i == count / 2) {
                lp.height = mCenterHeight;
            }
            addView(view, lp);
            final int position = i;
            view.requestFocus();
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mViewPager != null) {
                        mViewPager.setCurrentItem(position, false);
                    } else {
                        itemSelected(position);
                    }
                }
            });
        }
        if (viewPager != null) {
            viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    itemSelected(position);
                }
            });
        }
        // 默认选中第一个
        mAdapter.onTabSelected(getChildAt(mPosition), mPosition);
    }

    public void setAdapter(TabAdapter adapter) {
        setAdapter(adapter, null);
    }

    /**
     * 条目选中
     */
    private void itemSelected(int position) {
        if (mPosition != position) {
            mAdapter.onTabReset(getChildAt(mPosition), mPosition);
            mPosition = position;
            mAdapter.onTabSelected(getChildAt(mPosition), mPosition);
        }
    }

}
